package com.linkage.jz.bankruptcy.fragment.Vote;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.PDFViewActivity;
import com.linkage.jz.bankruptcy.activity.Vote.ProVoteEditVoteActivity;
import com.linkage.jz.bankruptcy.activity.Vote.ProVoteMyGroupsActivity;
import com.linkage.jz.bankruptcy.activity.Vote.ProVoteVoteDetailActivity;
import com.linkage.jz.bankruptcy.bean.Vote.ProVote;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteFile;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVoterGroup;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVoterMyGroup;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.utils.BundleUtils;
import com.linkage.jz.bankruptcy.utils.LogUtil;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.widget.ProVoteGroupListDialog;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.jz.bankruptcy.widget.PublicProLongNoticeDialog;
import com.linkage.jz.bankruptcy.widget.datepicker.DateFormatUtils;
import com.linkage.seapeak.activity.user.ImageShowActivity;
import com.linkage.seapeak.utils.CommonUtils;
import com.linkage.seapeak.utils.FileUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static android.app.Activity.RESULT_OK;

/**
 * 债权人表决详情页Tab之表决详情
 */
public class ProVoteDetailFragment extends ProVoteBaseFragment implements View.OnClickListener {

    public static final String ARG_PARAM = "arg_param";

    private static final int OPERATE_JOIN = 1;
    private static final int OPERATE_QUIT = 2;
    private static final int OPERATE_JOIN_VOTE = 3;
    private ProVote proVote;
    private View rootView;

    private RelativeLayout layoutNote;
    private TextView textNote;//债权人表决详情页顶部红色备注
    private LinearLayout layoutStatus;
    private TextView textStatus;//债权人表决详情页顶部状态
    private LinearLayout layoutGroupVoteMoney;//分组表决金额
    private LinearLayout layoutRevoke;
    private TextView textRevoke;//管理人表决详情页 是否撤销字段
    private LinearLayout layoutType;
    private TextView textType;//管理人表决详情页 表决类型
    private TextView textTitle;
    private TextView textContent;
    private TextView textRule;
    private TextView textRuleDesc;
    private RecyclerView fileRecyclerView;
    private RecyclerView fileImageRecyclerView;
    private RecyclerView groupRecyclerView;
    private TextView textStartTime;
    private TextView textEndTime;
    private TextView seeVotersDetails;
    private LinearLayout layoutAttendNotifyTime;//通知时间
    private TextView textAttendNotifyTime;
    private LinearLayout layoutPreAttendHour;//确认参会时间预设
    private TextView textPreAttendHour;
    private LinearLayout layoutUpload;

    private String fileUrl,fileName;

    private Button btnOperateQuit;//退出参会
    private Button btnOperateVote;//表决
    private Button btn_operate_jionvote;//参会表决
    private Button btn_operate_join;//参会
    private ProVoteGroupListDialog groupListDialog;
    private PublicProLongNoticeDialog noticeDialog;
    private PublicNoticeDialog makesureDialog;
    private String meetingSureStr = "请您仔细查阅公告的表决事项详情，您可在通知的时间内提前确定是否参会，并在表决开始后行使表决权，或于表决开始后直接参会表决。本表决事项适用《最高人民法院关于适用<中华人民共和国企业破产法>若干问题的规定（三）》第十四条，“债权人委员会决定所议事项应获得全体成员过半数通过”";

    public static ProVoteDetailFragment newInstance() {
        ProVoteDetailFragment fragment = new ProVoteDetailFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        proVote = BundleUtils.getProVote();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_pro_vote_vote_detail, container, false);
        }
        seeVotersDetails = rootView.findViewById(R.id.pro_vote_detail_see_voters);
        seeVotersDetails.setOnClickListener(this);
        /*if(proVote.getStatus() == 4 || proVote.getStatus() == 6 || proVote.getStatus() == 7) {//4-已撤销 6-未通过 7-已删除
            seeVotersDetails.setVisibility(View.GONE);
        }*/
        seeVotersDetails.setVisibility(View.GONE);
        initView(rootView);
        bindData(proVote);
        return rootView;
    }

    private void initView(View view) {
        layoutNote = view.findViewById(R.id.layout_note);
        textNote = view.findViewById(R.id.text_note);
        layoutStatus = view.findViewById(R.id.layout_status);
        textStatus = view.findViewById(R.id.text_status);
        layoutGroupVoteMoney = view.findViewById(R.id.layout_group_vote_money);
        layoutRevoke = view.findViewById(R.id.layout_revoke);
        textRevoke = view.findViewById(R.id.text_revoke);
        layoutType = view.findViewById(R.id.layout_type);
        textType = view.findViewById(R.id.text_type);
        textTitle = view.findViewById(R.id.text_title);
        textContent = view.findViewById(R.id.text_content);
        textRule = view.findViewById(R.id.text_rule);
        textRuleDesc = view.findViewById(R.id.text_rule_desc);
        fileRecyclerView = view.findViewById(R.id.recyclerView_file);
        fileImageRecyclerView = view.findViewById(R.id.recyclerView_file_image);
        groupRecyclerView = view.findViewById(R.id.recyclerView_group);
        textStartTime = view.findViewById(R.id.text_start_time);
        textEndTime = view.findViewById(R.id.text_end_time);
        layoutAttendNotifyTime = view.findViewById(R.id.layout_attend_notify_time);
        textAttendNotifyTime = view.findViewById(R.id.pro_vote_detail_send_notify_time);
        layoutPreAttendHour = view.findViewById(R.id.layout_pre_attend_time);
        textPreAttendHour = view.findViewById(R.id.text_pre_attend_hour);
        layoutUpload = view.findViewById(R.id.layout_upload);
        btnOperateQuit = view.findViewById(R.id.btn_operate_quit);
        btnOperateVote = view.findViewById(R.id.btn_operate_vote);
        btn_operate_jionvote= view.findViewById(R.id.btn_operate_jionvote);
        btn_operate_join= view.findViewById(R.id.btn_operate_join);
        btnOperateQuit.setOnClickListener(this);
        btnOperateVote.setOnClickListener(this);
        btn_operate_jionvote.setOnClickListener(this);
        btn_operate_join.setOnClickListener(this);
        rootView.setVisibility(View.INVISIBLE);
    }

    private void bindData(ProVote proVote) {
        rootView.setVisibility(View.VISIBLE);
        if(proVote.getStatus() == 1 || proVote.getStatus() == 2) { //未开始 进行中 显示红色参会编号区域
            if(proVote.getGroupInfo() != null && proVote.getGroupInfo().size() > 0) {
                if(TextUtils.isEmpty(proVote.getGroupInfo().get(0).getMeetingNumber())) {
                    layoutNote.setVisibility(View.GONE);
                }else {
                    layoutNote.setVisibility(View.VISIBLE);
                    textNote.setText("您的参会编号为" + proVote.getGroupInfo().get(0).getMeetingNumber() +"号，如至现场参会，请按该编号接受管理人的指引。");
                }
            }else {
                layoutNote.setVisibility(View.GONE);
            }
        }else {
            layoutNote.setVisibility(View.GONE);
        }
        //详情界面的状态
        boolean meeting = false;
        boolean voteOne = false;
        boolean voteAll = true;
        if(proVote.getGroupInfo() != null && proVote.getGroupInfo().size() > 0) {
            for(ProVoteVoterMyGroup group : proVote.getGroupInfo()) {
                if("1".equalsIgnoreCase(group.isMeeting())||"true".equalsIgnoreCase(group.isMeeting())) {
                    meeting = true;//是否参会，true 是，false否。如果是多个组，那么只要有一个参会标记为true即为已参会
                    break;
                }
            }
            for(ProVoteVoterMyGroup group : proVote.getGroupInfo()) {
                if("1".equalsIgnoreCase(group.isVote())||"true".equalsIgnoreCase(group.isVote())) {
                    voteOne = true;//是否有一个表决，true 是，false否。如果是多个组，那么只要有一个表决voteOne=true
                    break;
                }
            }
            for(ProVoteVoterMyGroup group : proVote.getGroupInfo()) {
                if("0".equalsIgnoreCase(group.isVote())||"false".equalsIgnoreCase(group.isVote())) {
                    voteAll = false;//是否全部表决，true 是，false否。如果是多个组，那么只要有一个没有表决voteAll=false
                    break;
                }
            }
        }

        int statusColor = getResources().getColor(R.color.pro_task_status_ywc);
        String statusText = "";
        switch (proVote.getStatus()) {
            case 0:
                statusColor = getResources().getColor(R.color.pro_vote_status_wfc);
                statusText = "未发出";
                layoutRevoke.setVisibility(View.GONE);
                break;
            case 1://未开始
                statusColor = getResources().getColor(R.color.pro_task_status_ywc);
                if(meeting) {
                    statusText = "已确认参会";
                }else {
                    statusText = "待参会";
                }
                layoutRevoke.setVisibility(View.GONE);
                break;
            case 2://进行中
                statusColor = getResources().getColor(R.color.pro_task_status_ywc);
                if(meeting) {
                    if(voteAll) {
                        statusText = "已表决";
                    }else {
                        if(voteOne) {
                            statusText = "已参会未表决";
                        }else {
                            statusText = "已参会未表决";
                        }
                    }
                }else {
                    statusText = "待参会表决";
                }
                layoutRevoke.setVisibility(View.GONE);
                break;
            case 3://已结束的
                statusColor = getResources().getColor(R.color.pro_task_status_wcl);
                if(meeting) {
                    if(voteAll) {
                        statusText = "已表决";
                    }else {
                        if(voteOne) {
                            statusText = "已参会未表决";
                        }else {
                            statusText = "已参会未表决";
                        }
                    }
                }else {
                    statusText = "未参会";
                }
                layoutRevoke.setVisibility(View.GONE);
                break;
            case 4:
                statusColor = getResources().getColor(R.color.pro_vote_status_ycx);
                statusText = "已撤销";
                layoutRevoke.setVisibility(View.VISIBLE);//TODO 目前看到接口没有返回撤销说明，返回的话此处显示，并textRevoke.setText()
                textRevoke.setText(TextUtils.isEmpty(proVote.getCancel()) ? "无" : proVote.getCancel());
                break;
            case 5:
                statusColor = getResources().getColor(R.color.pro_vote_status_ytg);
                statusText = "已通过";
                layoutRevoke.setVisibility(View.GONE);
                break;
            case 6:
                statusColor = getResources().getColor(R.color.pro_vote_status_wtg);
                statusText = "未通过";
                layoutRevoke.setVisibility(View.GONE);
                break;
            case 7:
                statusColor = getResources().getColor(R.color.pro_vote_status_ysc);
                statusText = "已删除";
                layoutRevoke.setVisibility(View.GONE);
                break;
        }
        layoutStatus.setVisibility(View.VISIBLE);
        textStatus.setText(Html.fromHtml("您的状态：<font color='"+ statusColor +"'>" + statusText + "</font>"));

        layoutGroupVoteMoney.removeAllViews();

        if(proVote.getGroupInfo() != null && proVote.getGroupInfo().size() > 0) {
            textStatus.setVisibility(View.VISIBLE);
            for(ProVoteVoterMyGroup group : proVote.getGroupInfo()) {
                TextView text = new TextView(getActivity());
                text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
                String p = CommonUtils.resetMoney(group.getVoteMoney());
                text.setText((group.getName() == null ? "" : group.getName()) + "此次您的表决金额为：" + p + "元");
                layoutGroupVoteMoney.addView(text);
            }
        }
        if(proVote.getNoGroupData())
        {
            textStatus.setVisibility(View.GONE);
        }else{
            textStatus.setVisibility(View.VISIBLE);
        }
        layoutType.setVisibility(View.GONE);//管理员显示类型
        textTitle.setText(proVote.getTitle());
        textContent.setText(proVote.getContent());
        textRule.setText(proVote.getRuleName());
        textRuleDesc.setText(proVote.getRuleExplain());

        VoteFileAdapter voteFileAdapter = new VoteFileAdapter(proVote.getFiles());
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getActivity());
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        fileRecyclerView.setLayoutManager(layoutManager1);
        fileRecyclerView.setAdapter(voteFileAdapter);

        VoteFileImageAdapter voteFileImageAdapter = new VoteFileImageAdapter(proVote.getFiles());
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getActivity());
        layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        fileImageRecyclerView.setLayoutManager(layoutManager2);
        fileImageRecyclerView.setAdapter(voteFileImageAdapter);

        textStartTime.setText(DateFormatUtils.long2Str(proVote.getStartTime(), true));
        textEndTime.setText(DateFormatUtils.long2Str(proVote.getEndTime(), true));

        List<ProVoteVoterGroup> getGroupstem  = new ArrayList<ProVoteVoterGroup>();
        if(proVote.getGroups() == null)
        {
            ProVoteVoterGroup tmp = new ProVoteVoterGroup();
            tmp.setName("总人数：");
            tmp.setMemberCount(proVote.getMemberTotal());
            getGroupstem.add(tmp);
        }else{
            getGroupstem = proVote.getGroups();
        }
        VoteGroupAdapter voteGroupAdapter = new VoteGroupAdapter(getGroupstem);
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getActivity());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        groupRecyclerView.setLayoutManager(layoutManager3);
        groupRecyclerView.setAdapter(voteGroupAdapter);

        layoutAttendNotifyTime.setVisibility(View.GONE);
        layoutPreAttendHour.setVisibility(View.GONE);
        layoutUpload.setVisibility(View.GONE);

        if("1".equalsIgnoreCase(proVote.getAuth().getQuitMeeting())||"true".equalsIgnoreCase(proVote.getAuth().getQuitMeeting())) {
            btnOperateQuit.setVisibility(View.VISIBLE);
        }else{
            btnOperateQuit.setVisibility(View.GONE);
        }

        if("1".equalsIgnoreCase(proVote.getAuth().getDoVote() )||"true".equalsIgnoreCase(proVote.getAuth().getDoVote() )) {
            btnOperateVote.setVisibility(View.VISIBLE);
        }else{
            btnOperateVote.setVisibility(View.GONE);
        }

        if("1".equalsIgnoreCase(proVote.getAuth().getJoinMeeting() )||"true".equalsIgnoreCase(proVote.getAuth().getJoinMeeting() )) {
            btn_operate_join.setVisibility(View.VISIBLE);
        }else{
            btn_operate_join.setVisibility(View.GONE);
        }


        if("1".equalsIgnoreCase(proVote.getAuth().getMeetingVote() )||"true".equalsIgnoreCase(proVote.getAuth().getMeetingVote() )) {
            btn_operate_jionvote.setVisibility(View.VISIBLE);
        }else{
            btn_operate_jionvote.setVisibility(View.GONE);
        }

//        if(proVote.getStatus() == 1 || proVote.getStatus() == 2) {//未开始、进行中
//            if(proVote.getAuth().getQuitMeeting() == 1) {
//                btnOperateQuit.setText("退出参会");
//                btnOperateQuit.setVisibility(View.VISIBLE);
//                if(proVote.getAuth().getDoVote() == 1) {
//
//                    btnOperateVote.setVisibility(View.VISIBLE);
//
//                }else {
//                    btnOperateQuit.setVisibility(View.GONE);
//                    btnOperateVote.setVisibility(View.VISIBLE);
//                }
//            }else if(proVote.getAuth().getJoinMeeting() == 1) {
//                btnOperateQuit.setText("参会");
//                btnOperateQuit.setVisibility(View.VISIBLE);
//                btnOperateVote.setVisibility(View.GONE);
//            }else if(proVote.getAuth().getMeetingVote() == 1) {
//                btnOperateQuit.setText("参会表决");
//                btnOperateQuit.setVisibility(View.VISIBLE);
//                btnOperateVote.setVisibility(View.GONE);
//            }else if(proVote.getAuth().getDoVote() == 1) {
//                btnOperateQuit.setVisibility(View.GONE);
//                btnOperateVote.setVisibility(View.VISIBLE);
//            }else {
//                btnOperateQuit.setVisibility(View.GONE);
//                btnOperateVote.setVisibility(View.GONE);
//            }
//        }else {
//            btnOperateQuit.setVisibility(View.GONE);
//            btnOperateVote.setVisibility(View.GONE);
//        }
    }

    /**
     * 参与or退出表决
     * @param operate OPERATE_JOIN 参会 OPERATE_QUIT 退出参会 OPERATE_JOIN_VOTE 参会表决
     */
    private void attendOrQuitVote(int operate) {
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            return;
        }
        showLoading();
        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken", "");
        switch (operate) {
            case OPERATE_JOIN:
                Network.getAPIService()
                        .getVoteMeeting_Creditor(userToken, proVote.getId())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseSubscriber<JsonObject>() {
                            @Override
                            public void onNext(JsonObject json) {
                                hideLoading();
                                int code = json.get("code").getAsInt();
                                if (code != 0) {
                                    super.onNext(json);
                                    return;
                                }
                                LogUtil.e("--response--", json.toString());
                                /*btnOperateQuit.setText("退出参会");
                                btnOperateQuit.setVisibility(View.VISIBLE);
                                btnOperateVote.setVisibility(View.GONE);*/
                                ((ProVoteVoteDetailActivity) getActivity()).refreshTab();
                            }

                            @Override
                            public void onCompleted() {
                                hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                hideLoading();
                            }
                        });
                break;
            case OPERATE_QUIT:
                Network.getAPIService()
                        .getVoteExitMeeting_Creditor(userToken, proVote.getId())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseSubscriber<JsonObject>() {
                            @Override
                            public void onNext(JsonObject json) {
                                hideLoading();
                                int code = json.get("code").getAsInt();
                                if (code != 0) {
                                    super.onNext(json);
                                    return;
                                }
                                LogUtil.e("--response--", json.toString());
                                /*btnOperateQuit.setText("参会");
                                btnOperateQuit.setVisibility(View.VISIBLE);
                                btnOperateVote.setVisibility(View.GONE);*/
                                ((ProVoteVoteDetailActivity) getActivity()).refreshTab();
                            }

                            @Override
                            public void onCompleted() {
                                hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                hideLoading();
                            }
                        });
                break;
            case OPERATE_JOIN_VOTE:
                Network.getAPIService()
                        .getVoteMeeting_Creditor(userToken, proVote.getId())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseSubscriber<JsonObject>() {
                            @Override
                            public void onNext(JsonObject json) {
                                hideLoading();
                                int code = json.get("code").getAsInt();
                                if (code != 0) {
                                    super.onNext(json);
                                    return;
                                }
                                LogUtil.e("--response--", json.toString());
                                Intent intent;
                                if (proVote.isGrouped()) {
                                    intent = new Intent(getActivity(), ProVoteMyGroupsActivity.class);
                                    intent.putExtra("voteId", proVote.getId());
                                    intent.putExtra("creditorId", (proVote.getGroupInfo() != null && proVote.getGroupInfo().size() > 0) ? proVote.getGroupInfo().get(0).getCreditorId() : "");
                                    getActivity().startActivityForResult(intent, ProVoteEditVoteActivity.REQUEST_CODE_VOTE);
                                } else {
                                    intent = new Intent(getActivity(), ProVoteEditVoteActivity.class);
                                    intent.putExtra("vote", false);
                                    intent.putExtra("voteId", proVote.getId());
                                    getActivity().startActivityForResult(intent, ProVoteEditVoteActivity.REQUEST_CODE_VOTE);
                                }
                            }

                            @Override
                            public void onCompleted() {
                                hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                hideLoading();
                            }
                        });
                break;
            }
    }

    /**
     * 参考资料单行文件Item
     */
    class VoteFileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        List<ProVoteFile> list;

        public VoteFileAdapter(List<ProVoteFile> data) {
            List<ProVoteFile> temp = new ArrayList<>();
            if(data != null)
                temp.addAll(data);
            this.list = getNotImageFile(temp, getImageFile(temp));
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new VoteFileViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.adapter_pro_vote_detail_item_file, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            VoteFileViewHolder holder = (VoteFileViewHolder)viewHolder;
            ProVoteFile item = list.get(i);
            holder.fileNameText.setText(item.getAttachmentName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(item !=null &&  item.getAttachmentSuffix()!=null) {
                        if(item.getAttachmentSuffix().equalsIgnoreCase("doc")
                                || item.getAttachmentSuffix().equalsIgnoreCase("docx")) {
                            fileUrl = item.getAttachmentPreviewAddress();
                            fileName = item.getAttachmentName();
                            makesureDialog = new PublicNoticeDialog(getActivity(), onClickListener,
                                    1, "提示", "确定下载该文件吗？",
                                    "取消", "确定");
                            makesureDialog.show();
                        }else if(item.getAttachmentSuffix().equalsIgnoreCase("pdf")){
                            Intent intent = new Intent(getActivity(), PDFViewActivity.class);
                            intent.putExtra("url", item.getAttachmentPreviewAddress());
                            getActivity().startActivity(intent);
                        }
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        class VoteFileViewHolder extends RecyclerView.ViewHolder {
            TextView fileNameText;
            public VoteFileViewHolder(@NonNull View itemView) {
                super(itemView);
                fileNameText = itemView.findViewById(R.id.item_file_cover);
            }
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_close:
                    makesureDialog.dismiss();
                    break;
                case R.id.btn_chancel_notice:
                    makesureDialog.dismiss();
                    break;
                case R.id.btn_save_notice:
                    FileUtils.saveNetFileToSD(getActivity(), fileUrl ,fileName);
                    makesureDialog.dismiss();
                    break;
            }
        }
    };
    /**
     * 参考资料图片Item
     */
    class VoteFileImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        List<ProVoteFile> list;
        ArrayList<String> urlList = new ArrayList<>();//传给大图页的
        public VoteFileImageAdapter(List<ProVoteFile> data) {
            List<ProVoteFile> temp = new ArrayList<>();
            if(data != null)
                temp.addAll(data);
            this.list = getImageFile(temp);
            LogUtil.e("--list--", ""+list.size());
            if(null != list && list.size() > 0) {
                for(ProVoteFile file : list) {
                    String url = file.getAttachmentPreviewAddress();
                    if(url != null && url.startsWith("http")) {
                        urlList.add(url);
                    }
                }
            }
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new VoteFileImageViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.adapter_pro_vote_detail_item_file_image, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
            VoteFileImageViewHolder holder = (VoteFileImageViewHolder)viewHolder;
            ProVoteFile item = list.get(i);
            Glide.with(getActivity()).load(item.getAttachmentPreviewAddress())
                    .placeholder(R.drawable.icon_stub)
                    .error(R.drawable.icon_stub).into(holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageShowActivity.start(getActivity(), urlList, i, 0);
                }
            });
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        class VoteFileImageViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            public VoteFileImageViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.item_image);
            }
        }
    }

    /**
     * 表决人员（分组）单行Item
     */
    class VoteGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        List<ProVoteVoterGroup> list;

        public VoteGroupAdapter(List<ProVoteVoterGroup> data) {
            List<ProVoteVoterGroup> temp = new ArrayList<>();
            if(data != null)
                temp.addAll(data);
            this.list = temp;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new VoteGroupViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.adapter_pro_vote_detail_item_group, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            VoteGroupViewHolder holder = (VoteGroupViewHolder)viewHolder;
            ProVoteVoterGroup item = list.get(i);
            if(proVote.isGrouped()) {

                holder.groupStatusText.setVisibility(View.VISIBLE);
            }else {
//                holder.groupNameText.setText("人员总数（" + item.getMemberTotal() + "人）");
                holder.groupStatusText.setVisibility(View.GONE);
            }
            holder.groupNameText.setText(item.getName()+"（" + item.getMemberCount() + "人）");
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        class VoteGroupViewHolder extends RecyclerView.ViewHolder {
            TextView groupNameText;
            TextView groupStatusText;
            public VoteGroupViewHolder(@NonNull View itemView) {
                super(itemView);
                groupNameText = itemView.findViewById(R.id.item_group_name);
                groupStatusText = itemView.findViewById(R.id.item_group_vote_status);
            }
        }
    }

    /**
     * @param list 详情里面的文件列表
     * @return 文件当中是图片类型的文件列表
     */
    private List<ProVoteFile> getImageFile(List<ProVoteFile> list) {
        List<ProVoteFile> images = new ArrayList<>();
        if(list != null && list.size() > 0) {
            for(ProVoteFile voteFile : list) {
                String suffix = voteFile.getAttachmentSuffix();
                if(null != suffix && (suffix.equalsIgnoreCase("jpg") || suffix.equalsIgnoreCase("jpeg")
                        || suffix.equalsIgnoreCase("bmp") || suffix.equalsIgnoreCase("gif") || suffix.equalsIgnoreCase("png"))) {
                    images.add(voteFile);
                }
            }
        }
        return images;
    }
    /**
     * @param list 详情里面的文件列表
     * @param images 文件当中是图片类型的文件列表
     * @return 文件当中非图片类型的文件列表
     */
    private List<ProVoteFile> getNotImageFile(List<ProVoteFile> list, List<ProVoteFile> images) {
        if(null != list && list.size() > 0) {
            if(null != images && images.size() > 0) {
                list.removeAll(images);
            }
        }
        return list;
    }

    /**
     * 1个分组以上的 需要弹出选择分组的弹出框
     */
    private void showChooseGroupDialog(){
        groupListDialog = new ProVoteGroupListDialog(getActivity(), proVote.getGroups(), new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), proVote.getGroups().get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        if(groupListDialog != null && !groupListDialog.isShowing()) {
            groupListDialog.show();
        }
    }

    private void goVote() {
        Intent intent;
        if(proVote.isGrouped()) {
            intent = new Intent(getActivity(), ProVoteMyGroupsActivity.class);
            intent.putExtra("voteId", proVote.getId());
            intent.putExtra("creditorId", (proVote.getGroupInfo() != null && proVote.getGroupInfo().size() > 0) ? proVote.getGroupInfo().get(0).getCreditorId() : "");
            getActivity().startActivityForResult(intent, ProVoteEditVoteActivity.REQUEST_CODE_VOTE);
        }else {
            intent = new Intent(getActivity(), ProVoteEditVoteActivity.class);
            intent.putExtra("vote", false);
            intent.putExtra("voteId", proVote.getId());
            getActivity().startActivityForResult(intent, ProVoteEditVoteActivity.REQUEST_CODE_VOTE);
        }
    }

    @Override
    public void onClick(View v) {
        final Intent intent = null;
        switch (v.getId()) {
            case R.id.pro_vote_detail_see_voters:
                /*if(proVote.getGroupInfo() != null && proVote.getGroupInfo().size() > 0) {
                    ProVoteVotersDetailActivity.showVoters(getActivity());
                }else {
                    ToastUtil.showShort("groupInfo返回为空");
                }*/
                break;
            case R.id.btn_operate_join:
                if(noticeDialog == null || !noticeDialog.isShowing()) {
                    noticeDialog = new PublicProLongNoticeDialog(getActivity(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (v.getId()) {
                                case R.id.ll_close:
                                case R.id.ll_close_lx:
                                case R.id.btn_chancel_notice_lx:
                                    noticeDialog.dismiss();
                                    break;
                                case R.id.btn_save_notice_lx:
                                    noticeDialog.dismiss();
                                    attendOrQuitVote(OPERATE_JOIN);
                                    break;
                            }
                        }
                    }, 1, "表决注意事项", meetingSureStr, "我已阅读并充分理解前述信息", "不同意", "同意");
                    noticeDialog.show();
                }
                break;
            case R.id.btn_operate_jionvote:
                if(noticeDialog == null || !noticeDialog.isShowing()) {
                    noticeDialog = new PublicProLongNoticeDialog(getActivity(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (v.getId()) {
                                case R.id.ll_close:
                                case R.id.ll_close_lx:
                                case R.id.btn_chancel_notice_lx:
                                    noticeDialog.dismiss();
                                    break;
                                case R.id.btn_save_notice_lx:
                                    noticeDialog.dismiss();
                                    attendOrQuitVote(OPERATE_JOIN_VOTE);
                                    break;
                            }
                        }
                    }, 1, "表决注意事项", getResources().getString(R.string.meeting_vote_notice_dialog_txt), "我已阅读并充分理解前述信息", "不同意", "同意");
                    noticeDialog.show();
                }
                break;
            case R.id.btn_operate_quit:
                if(makesureDialog == null || !makesureDialog.isShowing()) {
                    makesureDialog = new PublicNoticeDialog(getActivity(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (v.getId()) {
                                case R.id.ll_close:
                                    makesureDialog.dismiss();
                                    break;
                                case R.id.btn_chancel_notice:
                                    makesureDialog.dismiss();
                                    break;
                                case R.id.btn_save_notice:
                                    attendOrQuitVote(OPERATE_QUIT);
                                    makesureDialog.dismiss();
                                    break;
                            }
                        }
                    }, 1, "提示", "是否确认退出参会？", "取消", "确定");
                    makesureDialog.show();
                }
                break;
            case R.id.btn_operate_vote://表决
                if(noticeDialog == null || !noticeDialog.isShowing()) {
                    noticeDialog = new PublicProLongNoticeDialog(getActivity(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (v.getId()) {
                                case R.id.ll_close:
                                case R.id.ll_close_lx:
                                case R.id.btn_chancel_notice_lx:
                                    noticeDialog.dismiss();
                                    break;
                                case R.id.btn_save_notice_lx:
                                    noticeDialog.dismiss();
                                    goVote();
                                    break;
                            }
                        }
                    }, 1, "表决注意事项", getResources().getString(R.string.meeting_vote_notice_dialog_txt), "我已阅读并充分理解前述信息", "不同意", "同意");
                    noticeDialog.show();
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ProVoteEditVoteActivity.REQUEST_CODE_VOTE && resultCode == RESULT_OK) {
            //fetchData();
        }
    }
}
